﻿using System;
using System.IO;

namespace CsharpTrainer.Model.Entities
{
    public class Employee : ICloneable
    {
        public int EmployeeID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string Title { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string Country { get; set; }
        public string Notes { get; set; }

        public override string ToString()
        {
            string format = "Employee ID: {0}\nFirst Name: {1}\n"
                          + "Last Name: {2}\nTitle: {3}\nCity: {4}\n"
                          + "Region: {5}\nCountry: {6}\nNotes: {7}\n";

            return string.Format(format, EmployeeID, FirstName, LastName, Title, City, Region, Country, Notes);
        }

        public Object Clone()
        {  
            Employee cloned = new Employee();

            cloned.EmployeeID = this.EmployeeID;
            cloned.LastName = this.LastName;
            cloned.FirstName = this.FirstName;
            cloned.Title = this.Title;
            cloned.City = this.City;
            cloned.Region = this.Region;
            cloned.Country = this.Country;
            cloned.Notes = this.Notes;

            return cloned;  
        }

        public void SaveData(Stream s)
        {
            var w = new BinaryWriter(s);

            w.Write(EmployeeID);
            w.Write(LastName);
            w.Write(FirstName);
            w.Write(Title);
            w.Write(City);
            w.Write(Region);
            w.Write(Country);
            w.Write(Notes);

            w.Flush(); // Ensure the BinaryWriter buffer is cleared.
                        // We won't dispose/close it, so more data
        }

        public bool LoadData(Stream s)
        {
            var r = new BinaryReader(s);

            if (r.PeekChar() > -1)
            {
                EmployeeID = r.ReadInt32();
                LastName = r.ReadString();
                FirstName = r.ReadString();
                Title = r.ReadString();
                City = r.ReadString();
                Region = r.ReadString();
                Country = r.ReadString();
                Notes = r.ReadString();

                return true;
            }

            return false;
        }
    }
}
